package com.xu.book.servlet;

import com.xu.book.bean.Classes;
import com.xu.book.bean.Depart;
import com.xu.book.bean.Student;
import com.xu.book.service.IClassesService;
import com.xu.book.service.IDepartService;
import com.xu.book.service.IStudentService;
import com.xu.book.service.impl.ClassesServiceimpl;
import com.xu.book.service.impl.DepartServiceimpl;
import com.xu.book.service.impl.IStudentServiceimpl;
import com.xu.sys.bean.SysRole;
import com.xu.sys.bean.SysUser;
import com.xu.sys.service.IRoleService;
import com.xu.sys.service.IUserService;
import com.xu.sys.service.impl.IUserServiceimpl;
import com.xu.sys.service.impl.RoleServiceimpl;
import com.xu.sys.servlet.BaseServlet;
import com.xu.sys.utils.Constant;
import com.xu.sys.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "studentServlet",urlPatterns = {"/book/studentServlet"})
public class StudentServlet extends BaseServlet {
    private IStudentService service = new IStudentServiceimpl();
    public IDepartService departService = new DepartServiceimpl();
    public IClassesService classesService = new ClassesServiceimpl();
    public IUserService userService = new IUserServiceimpl();
    public IRoleService roleService = new RoleServiceimpl();
    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        super.list(req, resp);//处理分页相关的数据
        service.listPage(pageUtils);
        //存储数据到作用域
        req.setAttribute(Constant.LIST_PAGE_UTILS,pageUtils);
        req.getRequestDispatcher("/book/student/list.jsp").forward(req,resp);
    }

    @Override
    public void saveOrUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //查询所有的院系信息
        List<Depart> departs = departService.list();
        req.setAttribute("departs",departs);
        //判断是更新还是添加
        String id = req.getParameter("id");
        if (StringUtils.isNotEmpty(id)){
            //更新
            Student entity = service.findById(Integer.parseInt(id));
            //根据班级找到对应的院系信息
            Classes cls = classesService.findById(entity.getClassid());
            entity.setDepartId(cls.getDepartId());
            req.setAttribute(Constant.UPDATE_ENTITY,entity);
        }
        req.getRequestDispatcher("/book/student/addOrUpdate.jsp").forward(req,resp);

    }

    @Override
    public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String stuno = req.getParameter("stuno");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String talephone = req.getParameter("talephone");
        String email = req.getParameter("email");
        String wechat =req.getParameter("wechat");
        String address = req.getParameter("address");
        String classid = req.getParameter("classid");
        Student student = new Student();
        student.setName(name);
        student.setStuno(stuno);
        student.setGender(gender);
        student.setEmail(email);
        student.setTalephone(talephone);
        student.setWechat(wechat);
        student.setAddress(address);
        if (StringUtils.isNotEmpty(age)){
            student.setAge(Integer.parseInt(age));
        }
        if (StringUtils.isNotEmpty(classid)){
            Integer classId = Integer.parseInt(classid);
            //根据班级编号查找对应的班级信息
            Classes classes = classesService.findById(classId);
            //根据班级信息找到对应的院系信息
            Depart depart = departService.findById(classes.getDepartId());
            student.setClassname(classes.getName());
            student.setDepartname(depart.getName());
            student.setClassid(classId);
        }
        if (StringUtils.isNotEmpty(id)){
            //更新数据
            student.setId(Integer.parseInt(id));
            Student stu = service.findById(Integer.parseInt(id));
            SysUser user = userService.findById(stu.getAccount());
            user.setUsername(student.getName());
            user.setNickname(student.getName());
            user.setId(stu.getAccount());
            userService.updteById(user);//同步更新账号中的信息
            student.setAccount(stu.getAccount());
            service.updteById(student);//更新学生信息
        }else{
            //创建学生我们同步创建账号信息
            SysUser user = new SysUser();
            user.setUsername(student.getName());
            user.setNickname(student.getName());
            user.setPassword("123");
            //创建的角色需要我们指定默认的角色为学生
            SysRole role = new SysRole();
            role.setName(Constant.ROLE_STUDNET);
            List<SysRole> roles = roleService.list(role);
            if (roles != null && roles.size() > 0){
                role = roles.get(0);
                user.setRoleId(role.getId());
                user.setRolename(role.getName());
            }
            //创建账号
            userService.save(user);
            //查询刚刚创建的账号信息
            user = userService.findByName(user.getUsername());
            student.setAccount(user.getId());//绑定账号
            service.save(student);

        }
        //需要分配院系，查询所有的院系信息

        resp.sendRedirect("/book/studentServlet?action=list");
    }



    @Override
    public void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter(Constant.PARAMETER_ID);
        service.deleteById(Integer.parseInt(id));
        PrintWriter writer = resp.getWriter();
        writer.write("ok");
        writer.flush();
    }

    @Override
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    }
}
